package br.com.news.rss;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class UrlTest {

	public static String readLink(URL url) {
		String inputLine = null;
		String htmlTexto = "";
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));

			while ((inputLine = in.readLine()) != null) {
				htmlTexto += inputLine;
			}
			in.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return htmlTexto;
	}

	public static String getTexto(URL url) {
		String inputLine = null;
		StringBuffer htmlTexto = new StringBuffer();
		try {
			URLConnection yc = url.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					yc.getInputStream()));

			while ((inputLine = in.readLine()) != null) {
				htmlTexto.append(inputLine.replaceAll ("/\\s{2,}/g", "").replaceAll("  ", "").replaceAll("\t", ""));
			}
			in.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		htmlTexto.length();
		return getTexto(htmlTexto);
	}

	public static String getTitulo(StringBuffer texto) {

		String regex = "(<div class=\"materia-titulo\"> )(.|\n|\r|\\x0B)+?(</div>)";
		String regex2 = "[^<>]+(?=[<])";

		Pattern pattern = Pattern.compile(regex);

		Matcher matcher = pattern.matcher(texto);
		StringBuilder sb = new StringBuilder();
		while (matcher.find()) {
			String v = matcher.group();
			if (!v.isEmpty()) {
				sb.append(matcher.group()).append("\n");
			}
		}

		String texto2 = sb.toString();
		pattern = Pattern.compile(regex2);
		matcher = pattern.matcher(texto2);

		sb = new StringBuilder();
		while (matcher.find()) {
			String v = matcher.group();
			if (!v.isEmpty()) {
				sb.append(matcher.group()).append("\n");

			}
		}

		try {
			return sb.toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return texto2;

	}
	
	public static String getImagem(String texto) {
		String regex = "(?<=src=['|\"])[^'|\"]*?(?=['|\"])";

		Pattern pattern = Pattern.compile(regex);

		Matcher matcher = pattern.matcher(texto);
		StringBuilder sb = new StringBuilder();
		while (matcher.find()) {
			String v = matcher.group();
			if (!v.isEmpty()) {
				sb.append(matcher.group()).append("\n");
			}
		}

		try {
			return ConvertUtil.toUTF8(sb.toString().replaceAll("'", ""));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	public static String getTexto(StringBuffer texto) {
		//String regex = "(?<=<div class=\"materia-conteudo entry-content\" id=\"materia-letra\">).*(?=</div>)";
		String regex = "((<div (class=\"materia-conteudo entry-content\")(.)*?>)((.)*?)(((<div(.)*?>)((.)*?)((</div>){1}))*?)((</div>){1}))";

		String regex2 = "((<div (class=\"saibamais componente_materia\")(.)*?>)((.)*?)(((<div(.)*?>)((.)*?)((</div>){1}))*?)((</div>){1}))";

		String regex3 = "((<div (class=\"saibamais componente_materia expandido\")(.)*?>)((.)*?)(((<div(.)*?>)((.)*?)((</div>){1}))*?)((</div>){1}))";
		
		String regex4 = "((<script>)((.)*?)(((<script(.)*?>)((.)*?)((</script>){1}))*?)((</script>){1}))";


		Pattern pattern = Pattern.compile(regex);

		Matcher matcher = pattern.matcher(texto);
		StringBuilder sb = new StringBuilder();
		while (matcher.find()) {
			String v = matcher.group(1);
			if (!v.isEmpty()) {
				sb.append(matcher.group(1).replaceAll ("/\\s{2,}/g", "").replaceAll("  ", "").replaceAll("\t", "")).append("\n");
			}
		}


		String texto2 = sb.toString();
		pattern = Pattern.compile(regex2);
		matcher = pattern.matcher(texto2);

		sb = new StringBuilder();
		while (matcher.find()) {
			String v = matcher.group(1);
			if (!v.isEmpty()) {
				texto2 = texto2.replaceAll(matcher.group(1),"");

			}
		}

		String texto3 = texto2;
		pattern = Pattern.compile(regex3);
		matcher = pattern.matcher(texto3);

		sb = new StringBuilder();
		while (matcher.find()) {
			String v = matcher.group(1);
			if (!v.isEmpty()) {
				texto3 = texto3.replaceAll(matcher.group(1),"");
			}
		}
		
		String texto4 = texto3.replaceAll("\\{", "");
		pattern = Pattern.compile(regex4);
		matcher = pattern.matcher(texto4);

		sb = new StringBuilder();
		while (matcher.find()) {
			String v = matcher.group(1);
			if (!v.isEmpty()) {
				texto4 = texto4.replaceAll(matcher.group(1),"");
			}
		}
		
		try {
			return ConvertUtil.toUTF8(texto4.concat("</div>"));
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static int countMatches(String str, String sub) {
	    int count = 0;
	    int idx = 0;
	    while ((idx = str.indexOf(sub, idx)) != -1) {
	        count++;
	        idx += sub.length();
	    }
	    return count;
	}

}
